//
//  TaskCell.m
//  StationManager
//
//  Created by 张佳强 on 17/3/11.
//  Copyright © 2017年 Helong. All rights reserved.
//

#import "TaskCell.h"

@implementation TaskCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(unselected) name:@"unselected" object:nil];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selected) name:@"selected" object:nil];
        
        self.selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.selectButton setImage:[UIImage imageNamed:@"unselected_bg"] forState:UIControlStateNormal];
        [self.selectButton setImage:[UIImage imageNamed: @"selected_bg"] forState:UIControlStateSelected];
        [self.selectButton addTarget:self action:@selector(selectClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_selectButton];
        
        self.stationName = [[UILabel alloc]init];
        self.stationName.font = [UIFont systemFontOfSize:11.0];
        self.stationName.numberOfLines = 0;
        self.stationName.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.stationName];
        
        self.stationNumber = [[UILabel alloc]init];
        self.stationNumber.font = [UIFont systemFontOfSize:11.0];
        self.stationNumber.numberOfLines = 0;
        self.stationNumber.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.stationNumber];
        
        self.stationType = [[UILabel alloc]init];
        self.stationType.font = [UIFont systemFontOfSize:11.0];
        self.stationType.numberOfLines = 0;
        self.stationType.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.stationType];
        
        self.location = [[UILabel alloc]init];
        self.location.font = [UIFont systemFontOfSize:11.0];
        self.location.numberOfLines = 0;
        self.location.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.location];
        
        [self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@1);
            make.top.equalTo(@0);
            make.width.mas_equalTo(30);
            make.height.mas_equalTo(30);
        }];
        
        [self.stationName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.selectButton.mas_right).offset(1);
            make.top.equalTo(@0);
            make.width.mas_equalTo((SCREEN_WIDTH-36)/4);
            make.height.mas_equalTo(30);
        }];
        
        [self.stationNumber mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.stationName.mas_right).offset(1);
            make.top.equalTo(@0);
            make.width.mas_equalTo((SCREEN_WIDTH-36)/4);
            make.height.mas_equalTo(30);
        }];
        
        [self.stationType mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.stationNumber.mas_right).offset(1);
            make.top.equalTo(@0);
            make.width.mas_equalTo((SCREEN_WIDTH-36)/4);
            make.height.mas_equalTo(30);
        }];
        
        [self.location mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.stationType.mas_right).offset(1);
            make.top.equalTo(@0);
            make.width.mas_equalTo((SCREEN_WIDTH-36)/4);
            make.height.mas_equalTo(30);
        }];
    }
    return self;
}

#pragma mark - Notification - Delegate

////当cell不选中时
//- (void)unselected {
//    
//    self.selectButton.selected = NO;
//    [self.addSelectedDelegate deleteSelectedCell:self.cellTag];
//}
//
////当cell选中时
//- (void)selected {
//    
//    self.selectButton.selected = YES;
//    [self.addSelectedDelegate addSelectedCell:self.cellTag];
//    
//}

//点击选择按钮
- (void)selectClick {
    
    if (self.selectButton.selected) {
        self.selectButton.selected = NO;
        [self.addSelectedDelegate deleteSelectedCell:self.cellTag];
    }else{
        self.selectButton.selected = YES;
        [self.addSelectedDelegate addSelectedCell:self.cellTag];      
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
